package network.client;

import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import network.Network;
import network.NetworkEvent;
import network.NetworkListener;
import network.SendBatch;
import network.networkManager.NetworkManager;
import util.ConsoleMonitor;

public class Client implements NetworkListener
{
	public static void main(String[] args)
	{
		//System.out.println("client");
		new ConsoleMonitor("Client");
		System.out.println("starting client");
		try
		{
			Network nm = new Network(NetworkManager.clientPort);
			nm.addProfile(InetAddress.getLocalHost(), NetworkManager.serverPort);
			Charset c = Charset.forName("UTF-8");
			for(int i = 0; i < 7; i++)
			{
				SendBatch batch = new SendBatch();
				batch.add(c.encode("simple test message!!!").array(), true);
				System.out.println("batch = "+batch+"\n------------");
				nm.send(batch, InetAddress.getLocalHost());
				try
				{
					Thread.sleep(4000);
				}
				catch(InterruptedException d){}
			}
		}
		catch(IOException e){}
	}
	public void packetReceived(NetworkEvent e)
	{
		InetAddress address = e.getInetAddress();
		ByteBuffer buff = e.getPacketData();
		
		Charset c = Charset.forName("UTF-8");
		System.out.println("packet received, address = "+address);
		System.out.println(c.decode(buff));
	}
}
